<?php
// $Id$

/**
 * Controller_Cart 控制器
 */
//session_start();
class Controller_Cart extends Controller_Abstract
{

	function actionIndex()
	{
        // 为 $this->_view 指定的值将会传递数据到视图中
		# $this->_view['text'] = 'Hello!';
	}
	
	function actionAddItem()
	{
//		$this->initial();
		if(empty($_SESSION['cart']))
		{
			$this->initial();
		}
		$goods_onshelf_id = $this->_context->onshelf_goods_id;
		$amount = $this->_context->amount;
		$cart = $_SESSION["cart"];
		$items = $cart['items'];
//		$jsonitem = json_encode($items);
//		echo ($jsonitem);
		foreach($items as $items)
		{
			if($items['goods']['id'] == $goods_onshelf_id)
			{
				echo("duplicatedItem();");
				exit(1);
			}
		}
		$goods = $this->loadGoodsOnshelf($goods_onshelf_id,$amount);

		if(!empty($goods))
		{
			$cartItem = array('goods'=>$goods,'amount'=>$amount);
//			echo($cartItem['id']."<br>");
//			echo($cartItem['goods']->goods_onshelf_id);
			$cart["items"] = array_merge($cart["items"], array($cartItem));

			$_SESSION["cart"] = $cart;
			$json = json_encode($cart);
			
			echo "showCart(".$json.");";
		}
		else
		{
			echo "noItem();";
		}
		
	}

	function initial()
	{
		$cart= array();
		$cart['id']=date("His");
		$cart['items']=array();
		$_SESSION['cart'] = $cart;
	}
	
	function actionDeleteItem()
	{
		$onshelf_goods_id = $this->_context->onshelf_goods_id;
		$cart = $_SESSION["cart"];
		$i = 0;
		$pos = 0;
		for($i=0;$i<count($cart["items"]); $i=$i+1)
		{
			if($cart["items"][$i]['goods']['id'] == $onshelf_goods_id)
			{
				$pos = $i;
			}
		}
		array_splice($cart["items"], $pos, 1);
		$_SESSION["cart"] = $cart;
		$json = json_encode($cart);
		echo "showCart(".$json.");";
	}
	
	function actionChangeAmount()
	{
		$amount = $this->_context->amount;
		$id = $this->_context->id;
		$cart = $_SESSION["cart"];
		$items = $cart['items'];
		$i = 0;
		for($i=0;$i<count($cart["items"]);$i=$i+1)
		{
			if($cart["items"][$i]['goods']['id'] == $id)
			{
				$cart["items"][$i]['amount'] = $amount;
				$_SESSION["cart"] = $cart;
				$json = json_encode($cart);
				echo "showCart(".$json.");";
				exit(1);
			}
		}
			
	}
	function loadGoodsOnshelf($id,$amount)
	{
		$onshelf = GoodsOnshelf::find('goods_onshelf_id = ?', $id)->query();
		if($onshelf->stock >= $amount)
		{
			$goods = array();
			$goods_id = $onshelf->goods_id;
			$goodInfo = GoodsInfo::find('goods_id = ?', $goods_id)->query();
			$goods['id'] = $id;
			$goods['img'] = $goodInfo->small_img_urls;
			$goods['goodsname'] = $goodInfo->goods_name;
			$goods['market_price'] = $onshelf->market_price;
			$goods['mall_price'] = $onshelf->mall_price;
			$goods['exper'] = $onshelf->exp;
			$goods['stock'] = $onshelf->stock;
			return $goods;
		}
		else
		{
			return null;
		}
	}

/*	function save()
	{
		Cart::meta()->destroyWhere('user_id = ?', $this->_app->currentUserObject()->id());
		$cart = $_SESSION["cart"];
		if($cart->user_id == $this->_app->currentUserObject()->id())
		{
			foreach($cartItem as $cart["items"])
			{
				$user = $this->_app->currentUserObject();
				$arr = array("onshelf_goods_id" => $cartitem['id']);
				$cart = $user->createCartItem($arr, $cartItem['amount']);
				$cart->save();
			}
		}
	
	}
	
	function load()
	{
		$cart = array();
		$cart["user_id"] = $this->_app->currentUserObject()->id();
		$loadInfo = Cart::find("user_id = ?", $this->_app->currentUserObject()->id())->getAll();
		$cart["items"] = array();
		foreach($item as $loadinfo)
		{
			$goods = loadGoods($item->onshelf_goods_id);
			$cartItem = array('id'=>$item->onshelf_goods_id,
					'goods'=>$goods, 'amount'=>$item->amount);
			array_merge($cart["items"],array($cartitem));
		}
		$_SESSION["cart"] = $cart;
	}
*/
	function actionShowCart()
	{		
		if(empty($_SESSION['cart']))
		{
			$this->initial();
		}
		$cart = $_SESSION["cart"];
		$json = json_encode($cart);
		echo "showCart(".$json.");";
	}
/*	
	function checkout()
	{
		$cart = $_SESSION["cart"];
		$sum = 0;
		foreach($item as $cart["items"])
		{
			$pay = $item['goods']->mall_price * $item['amount'];
			$sum = $sum + $pay;
		}
		return $sum;
	}

	function actionInitial()
	{
		if(empty($this->_app->currentUserObject()))
		{
			$cart=array("user_id"=>'-1',"items"=>array());
			$_SESSION['cart'] = $cart;
		}
		else
		{
			load();
		}
	}

	function actionClearCart()
	{
		$cart["items"]=array();
		$_SESSION['cart'] = $cart;
	}

	function sessionTimeout()
	{
		if(empty($this->_app->currentUserObject()))
		{
			$cart=$_SESSION["cart"];
			setcookie();
		}
		else
		{
			save();
		}
	}
*/
	
}


